<?php
/*
 * 封装了jqGrid
 */
class Control_JQGrid extends QUI_Control_Abstract
{
    function render()
    {
        $context = QContext::instance();
        $_BASE_DIR = $context->baseDir();
        $class = $this->_extract('class');
        $data_url = $this->_extract('data_url');
        $edit_url = $this->_extract('edit_url');
        $view_url = $this->_extract('view_url');
        $create_url = $this->_extract('create_url');
        $operate_url = $this->_extract('operate_url');
        $id_field = $this->_extract('id_field');
        $pagesize = $this->_extract('pagesize');
        $caption = $this->_extract('caption');

        $cols = $this->_extract('cols');
        //$cols[] = array('label'=>'操作','name'=>'operate','width'=>90,'search'=>false);

        $id    = $this->id();
        $name  = $this->name();
        $value = $this->value;

        //日期选择器
        $datepickers = array();
        //生成 colModel
        $col_models = '';
        $col_names = '';
        foreach($cols as $colmodel)
        {
            $col_names .= "'" . $colmodel['label'] . "',";
            if(isset($colmodel['datepicker']) && $colmodel['datepicker']) $datepickers[] = 'sg_'.$colmodel['name'];
        }

        $col_models = json_encode($cols);
        $col_names = rtrim($col_names, ",");
        $datapickers_ids = implode(',#', $datepickers);

        $out = <<<EOT
<script src="{$_BASE_DIR}ui/jqGrid/jquery.jqGrid.js" type="text/javascript"></script>
<script src="{$_BASE_DIR}ui/jqGrid/js/jqModal.js" type="text/javascript"></script>
<script src="{$_BASE_DIR}ui/jqGrid/js/jqDnR.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function(){ 
  jQuery("#{$id}").jqGrid({
    url: "{$data_url}",
    datatype: 'json',
    jsonReader:{
    repeatitems: false,
    id: "{$id_field}"
    },
    height: '100%',
    width: 960,
forceFit:true,
caption: '{$caption}',
    shrinkToFit: true,
    mtype: 'GET',
    multiselect: true,
    multiboxonly: true,
    colNames :[{$col_names}],
    colModel :{$col_models},
    pager: jQuery('#{$id}_pager'),
    rowNum:{$pagesize},
    rowList:[{$pagesize},{$pagesize}*2,{$pagesize}*3],
    sortname: '{$id_field}',
    sortorder: "desc",
//    viewrecords: true,
    imgpath: '{$_BASE_DIR}ui/jqGrid/themes/steel/images'
  });
  jQuery("#{$id}").navGrid('#{$id}_pager',{
        edit:false,add:false,del:false,search:false
      }).navButtonAdd('#{$id}_pager',{
          caption:"详情", buttonimg:jQuery("#{$id}").getGridParam("imgpath")+"/find.gif", onClickButton: function(){
          var gr = jQuery("#{$id}").getGridParam("selarrrow");
            if(gr.length != 1){
                alert('请选择一行查看详情');
            }
            else{
                window.location.href = '{$view_url}&{$id_field}='+jQuery("#{$id}").getGridParam("selarrrow");
            };
         }, position:"last"
    }).navButtonAdd('#{$id}_pager',{
        caption:"添加", buttonimg:jQuery("#{$id}").getGridParam("imgpath")+"/row_add.gif", onClickButton: function(){
            window.location.href = '{$edit_url}';
        }, position:"last"
        }).navButtonAdd('#{$id}_pager',{
        caption:"修改", buttonimg:jQuery("#{$id}").getGridParam("imgpath")+"/row_edit.gif", onClickButton: function(){
        var gr = jQuery("#{$id}").getGridParam("selarrrow");
        if(gr.length != 1){
            alert('请选择一行进行修改');
        }
        else{
            //window.location.href = '{$edit_url}&{$id_field}='+jQuery("#{$id}").getGridParam("selarrrow");

        }
        }, position:"last"
        }).navButtonAdd('#{$id}_pager',{
          caption:"删除", buttonimg:jQuery("#{$id}").getGridParam("imgpath")+"/row_delete.gif", onClickButton: function(){
          var gr = jQuery("#{$id}").getGridParam("selarrrow");
          if( gr != '' ) {
            jQuery("#{$id}").delGridRow(gr,{top:150,left:200,msg:'确定要删除选定的行？' ,afterSubmit: function(xhr,postdata){ alert (xhr.responseTEXT); return [true]},
        url: '{$operate_url}'});
          } else {
            alert("请选择要删除的行！");
          };
         }, position:"last"
    });
    jQuery("#{$id}_search").filterGrid("#{$id}",{
        gridModel:true,
        gridNames:true,
        gridToolbar:true,
        enableSearch: true,
        enableClear:true,
        searchButton: '搜索',
        clearButton: '清除',
        autosearch: false
    });
    $('#{$datapickers_ids}').datepicker();
}); 
</script>
<div id="{$id}_search" class="search_bar"></div>
<table id="{$id}" class="scroll"></table> 
<div id="{$id}_pager" class="scroll" style="text-align:center;"></div> 
EOT;

        return $out;
    }
}

